#include "ScnsSt7796.c.h"

#if defined(SCNS_ST7796_ENABLE)&&SCNS_ST7796_ENABLE==1

#include "ScnsSmctp.h"
#include "ScnsTimer.h"
#include "spi.h"

const ScnsBspSt7796Config scnsBspSt7796Config[SCNS_ST7796_MAX]={
    {
        .dir=3,
        .horizon=320,
        .vertical=480,
        .horizonBias=0,
        .verticalBias=0,
        .bgr=0,
        .reverseColor=0,
    },
};

ScnsStatus scnsBspSt7796SetCs(ScnsSt7796Enum stN,uint8 cs)
{
    if(cs==0)
    {
        spi4Block();
        hspi4.Instance->CFG1=(hspi4.Instance->CFG1)&(~0X70000000UL)|(SPI_BAUDRATEPRESCALER_4);
        LCD_CS2_GPIO_Port->BSRR=(LCD_CS2_Pin<<16);
    }
    else
    {
        LCD_CS2_GPIO_Port->BSRR=(LCD_CS2_Pin);
    }
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspSt7796SetBackgroundLight(ScnsSt7796Enum stN,ScnsLcdBackgroundLightStatus status)
{
    LCD_BL_GPIO_Port->BSRR=((status==SCNS_LCD_BACKGROUND_LIGHT_ON?0:1))?(LCD_BL_Pin):(LCD_BL_Pin<<16);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspSt7796WReg(ScnsSt7796Enum stN,uint8 dat)
{
    LCD_DC_GPIO_Port->BSRR=(LCD_DC_Pin<<16);
    HAL_SPI_Transmit(&hspi4,&dat,1,500);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspSt7796WData(ScnsSt7796Enum stN,uint8 dat)
{
    LCD_DC_GPIO_Port->BSRR=(LCD_DC_Pin);
    HAL_SPI_Transmit(&hspi4,&dat,1,500);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspSt7796WData16S(ScnsSt7796Enum stN,uint16 dat[],uint32 len)
{
    LCD_DC_GPIO_Port->BSRR=(LCD_DC_Pin);
    HAL_SPI_Transmit(&hspi4,(uint8*)dat,sizeof(uint16)*len,500);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspSt7796WData16SAsync(ScnsSt7796Enum stN,uint16 dat[],uint32 len)
{
    LCD_DC_GPIO_Port->BSRR=(LCD_DC_Pin);
    HAL_SPI_Transmit_DMA(&hspi4,(uint8*)dat,sizeof(uint16)*len);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspSt7796RData(ScnsSt7796Enum stN,uint8 dat[],uint32 len)
{
    hspi4.Instance->CFG1=(hspi4.Instance->CFG1)&(~0X70000000UL)|(SPI_BAUDRATEPRESCALER_16);
    LCD_DC_GPIO_Port->BSRR=(LCD_DC_Pin);
    HAL_SPI_Receive(&hspi4,dat,len,500);
    return SCNS_STATUS_OK;
}

ScnsStatus scnsBspSt7796RDataAsync(ScnsSt7796Enum stN,uint8 dat[],uint32 len)
{
    hspi4.Instance->CFG1=(hspi4.Instance->CFG1)&(~0X70000000UL)|(SPI_BAUDRATEPRESCALER_16);
    LCD_DC_GPIO_Port->BSRR=(LCD_DC_Pin);
    HAL_SPI_Receive_DMA(&hspi4,dat,len);
    return SCNS_STATUS_OK;
}
#endif
